using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC.Models;
using Telerik.Web.Mvc.Extensions;

namespace MVC.Controllers
{
    public class Demo_4Controller : Controller
    {
        //
        // GET: /Demo_4/

        public ActionResult Grid()
        {
            return View(SessionCustomerRepository.CustomerList());
        }

        [HttpPost]
        public ActionResult Insert()
        {
            EditableCustomer customer = new EditableCustomer();

            //Attempt to update our model with the information that has been entered in the Grid
            if(TryUpdateModel(customer))
            {
                SessionCustomerRepository.Insert(customer);

                //GridRouteValues is an extension method which returns the route values defining the
                //Grid state. This includes the current page, sort expressions, filter expressions etc.
                return RedirectToAction("Grid", this.GridRouteValues());
            }

            //Model is invalid, return unchanged list
            return View("Grid", SessionCustomerRepository.CustomerList());
        }

        [HttpPost]
        public ActionResult Update(String id)
        {
            EditableCustomer customer = new EditableCustomer { CustomerID = id };

            //Attempt to update our model with the information that has been entered in the Grid
            if (TryUpdateModel(customer))
            {
                SessionCustomerRepository.Update(customer);

                //GridRouteValues is an extension method which returns the route values defining the
                //Grid state. This includes the current page, sort expressions, filter expressions etc.
                return RedirectToAction("Grid", this.GridRouteValues());
            }

            //Model is invalid, return unchanged list
            return View("Grid", SessionCustomerRepository.CustomerList());
        }

        [HttpPost]
        public ActionResult Delete(String id)
        {
            //Find the EditableCustomer we are trying to Delete
            EditableCustomer customer = SessionCustomerRepository.SelectOne(c => c.CustomerID == id);

            if (customer == null)
            {
                //A Customer to delete was not found, simply redisplay the Grid

                //GridRouteValues is an extension method which returns the route values defining the
                //Grid state. This includes the current page, sort expressions, filter expressions etc.
                return RedirectToAction("Grid", this.GridRouteValues());
            }

            //Delete the EditableCustomer from our list
            SessionCustomerRepository.Delete(customer);

            //Redisplay the Grid
            //GridRouteValues is an extension method which returns the route values defining the
            //Grid state. This includes the current page, sort expressions, filter expressions etc.
            return RedirectToAction("Grid", this.GridRouteValues());
        }
    }
}
